﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video.DirectShow;

namespace ComTools
{
    public partial class frmCamera : Form
    {
        public frmCamera()
        {
            InitializeComponent();
        }

        private void frmCamera_Load(object sender, EventArgs e)
        {

        }

        FilterInfoCollection videoDevices;

        private void BtnReload_Click(object sender, EventArgs e)
        {
            // 枚举所有视频输入设备
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count == 0)
                throw new ApplicationException();

            foreach(FilterInfo filter in videoDevices)
            {
                comboBox1.Items.Add(filter.Name + "-" + filter.MonikerString);
            }

        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            //videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
            //videoSource.DesiredFrameRate = 1;
            
            videoSourcePlayer1.VideoSource = videoSource;
            videoSourcePlayer1.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            //videoSourcePlayer1.Stop();
            videoSourcePlayer1.SignalToStop();
            videoSourcePlayer1.WaitForStop();
        }
    }
}
